import os
from random import randint
num=randint(0,100)
def data_write(user_path,time):
    time = str(time)
    with open(user_path, "a", encoding="UTF-8") as f:
        f.writelines(time+"\n")
def data_read(user_path):
    with open(user_path, "r", encoding="UTF-8") as f:
        round = f.read().split()
        for time in round:
            time = int(time)
    return round
username = input("玩家姓名:")
data_path = os.getcwd()+ "/game_data"
folder = os.path.exists(data_path)
if not folder:
    os.makedirs(data_path)
user_list = os.listdir(data_path)
user_path =("game_data/" + username + ".txt")
username_1 = (username+".txt")
if username_1 in user_list:
    print("欢迎回来 {} ,祝您游戏愉快!".format(username))
    print("猜猜数字是几？")
elif username_1 not in user_list:
    print("欢迎加入游戏 {} ,祝您游戏愉快!".format(username))
    with open(user_path, "a", encoding="UTF-8") as f_user:
        time = 1
        print("猜猜数字是几？")

game_times=0
avr_times=0
st_times=0
right_times=0
while True:
	choice = input("S:开始，E：结束游戏\n")
	if choice == "S":
		game_times += 1
		break
def guess(num):
	answer=int(input("请输入100以内的数字：\n"))
	global st_times
	global right_times
	global game_times
	st_times+=1
	if answer>num:
		print("太大了！")
		return False
	elif answer<num:
		print("太小了！")
		return False
	else:
		print("猜对咯！就是",answer)
		right_times += 1
		while True:
			choice = input("继续游戏请输入：go\n")
			if choice == "go":
				game_times += 1
			else:

				import os
				os._exit(0)
		return True

def game():
	while True:
		if guess(num):
			print("你一共玩了%d次,用了%d次机会,有%d次猜对了"%(game_times,st_times,right_times))
			break
game()
